/* eslint-disable @typescript-eslint/no-explicit-any */

export function isEmpty(value: string | number) {
  if (value === undefined || value === null || value === 'null' || value === '-' || value === '') {
    return true
  }
  return false
}

export function customToFixed(num: number, decimal: number) {
  return num.toCustomFixed(decimal)
}

export function toThousand(
  num: number | string | undefined,
  decimal?: number,
  preffix?: string | boolean,
  fill = '-'
) {
  if (num === undefined || num === null || num === 'null' || num === '-' || num === '') return fill
  num = Number(+num || 0)
  if (Number(decimal) > -1) {
    num = Number(num).toCustomFixed(decimal)
  }
  let preffixChar = preffix
  if (typeof preffix === 'boolean') {
    preffixChar = Number(num) === 0 ? '' : Number(num) >= 0 ? '+' : '-'
    num = String(num).replace(preffixChar, '')
  } else preffixChar = ''
  return preffixChar + String(num).replace(/^-?\d+/g, (m) => m.replace(/(?=(?!\b)(\d{3})+$)/g, ','))
}

export function toPercent(
  num: number | string | undefined,
  decimal = 0,
  preffix = false,
  suffix = '%',
  multiple = 100
) {
  if (num === undefined || num === null || num === 'null' || num === '-' || num === '') return '-'
  if (typeof num === 'string' && num.indexOf('%') > -1) num = num.replace('%', '')
  let str = (Number(num) * multiple).toCustomFixed(decimal)
  // let str = (
  //   Math.round(Number(+num + 'e' + decimal) * multiple) / Math.pow(10, decimal)
  // ).toFixed(decimal);
  if (preffix) {
    const preffixChar = Number(num) === 0 ? '' : Number(num) >= 0 ? '+' : '-'
    str = preffixChar + str.replace(preffixChar, '')
  }
  return str + suffix
}

export function showUpOrDown(num: number | undefined | string) {
  if (num === undefined || num === null || num === 'null' || num === '-' || num === '') return ''
  return Number(num) === 0
    ? ''
    : Number(num) >= 0
      ? "<span class='up-arrow'></span>"
      : "<span class='down-arrow'></span>"
}

export const toPercentHtml = (
  {
    num = '',
    decimal = 0,
    preffix = false,
    suffix = '%',
    multiple = 100
  }: {
    num?: number | string | undefined
    decimal?: number | undefined
    preffix?: boolean
    suffix?: string | undefined
    multiple?: number | undefined
  },
  title?: string | number | undefined
) => {
  let str = toPercent(num, decimal, preffix, suffix, multiple)
  // console.log('toPercentHtml', title, str, title !== '市占率');
  if (title === '市占率' || title === '份额') {
    str = str.replace('+', '')
  }
  if (
    title !== '市占率' &&
    title !== '份额' &&
    str &&
    str !== '-' &&
    Number(str.replace('%', '')) !== 0
  ) {
    const color = str.startsWith('-') ? '#FF5656' : '#35C08E'
    return `<span style="color:${color};">${str}</span>`
  }
  return str
}

export function strHide(str: string, start = 3, end = 7) {
  const pattern = `(\\S{${start}})\\S{${end - start}}(\\S{${str.length - end}})`
  const reg = RegExp(pattern)
  return str.replace(reg, '$1****$2')
}

export function toVideoUrl(url: string) {
  if (!url || url.startsWith('http')) return url
  return `/medium${url.startsWith('/') ? url : `/${url}`}`
}

/**
 * 参数对象拼装为uri
 * @param {Object} params URL参数
 */
export function queryEncoded(params: any) {
  let queryStr = ''
  for (const key in params) {
    const value = encodeURIComponent(params[key])
    if (value === '' || value === undefined) continue
    queryStr += '&' + key + '=' + value
  }
  return queryStr.substr(1)
}
// http://xxxxx?code=6e299c3e98b41a49a379d74e0d858bc2&state=1231#/login
// http://xxxxx/#/auth-redirect?code=6e299c3e98b41a49a379d74e0d858bc2&state=1231
export function parseUrl(url: string) {
  const params: any = {}
  const query: any = {}
  let h
  const arr = url.split('#')
  let path = arr[0]
  let hash = arr[1]

  const paths = path ? path.split('?') : []
  path = paths[0]
  let uri = (paths[1] && paths[1].split('&')) || []

  for (let i = 0; i < uri.length; i++) {
    h = uri[i].split('=')
    params[h[0]] = h[1]
  }

  const hashs = hash ? hash.split('?') : []
  hash = hashs[0]
  uri = (hashs[1] && hashs[1].split('&')) || []

  for (let j = 0; j < uri.length; j++) {
    h = uri[j].split('=')
    query[h[0]] = h[1]
  }

  return {
    path,
    params,
    hash,
    query
  }
}

export function arrInsert(arr: any[], index: number, item: any) {
  arr.splice(index, 0, item)
  return arr
}
